package com.bocai.grainshop.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bocai.common.valid.UpdateStuats;
import com.bocai.common.valid.UpdateVaild;
import com.bocai.grainshop.product.vo.BrandVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.bocai.grainshop.product.entity.BrandEntity;
import com.bocai.grainshop.product.service.BrandService;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author SunJinBo
 * @email 2374461077@qq.com
 * @date 2022-03-01 17:47:31
 */
@Slf4j
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/infos")
    public R info(@RequestParam("brandId") List<Long> brandId){
        log.info("参数{}"+brandId);
        List<BrandEntity> brand = brandService.getBrandsByIds(brandId);
        log.info("brand{}"+brand);
        return R.ok().put("brand",brand);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存 @Valid告诉springMVC这个字段需要校验
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody BrandEntity brand,BindingResult result){
        Map<String,String> map = new HashMap<>();
        if(result.hasErrors()){
            //获取校验的错误结果
            result.getFieldErrors().forEach((itme)->{
                //FieldError获取到错误提示
                String message = itme.getDefaultMessage();
                //获取错误的属性的名字
                String field = itme.getField();
                map.put(field,message);
            });
            log.info("sss");
            return R.error(400,"名牌不合法").put("error",map);
        }else {
            brandService.save(brand);
        }

        return R.ok();
    }

    /**
     * 修改  @Validated  分组校验注解
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateVaild.class) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改状态  @Validated  分组校验注解
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated(UpdateStuats.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
